package com.blue.core.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeBuilder
{
	private TreeBuilder()
	{
	}
	
	public static <T extends ITreeItem> Map<String, List<T>> buildTree(List<T> list)
	{
		Map<String, List<T>> map = new HashMap<>();
		if (list == null || list.isEmpty())
			return map;
		
		for (T t : list)
		{
			String parentId = t.getParentId() == null ? "0" : String.valueOf(t.getParentId());
			List<T> tmp = map.get(parentId);
			if (tmp == null)
			{
				tmp = new ArrayList<>();
				map.put(parentId, tmp);
			}
			tmp.add(t);
		}
		
		return map;
	}
	
}
